﻿Imports DevExpress.XtraRichEdit.API.Native


Public Class UcnRTFeditor
    'represents Rich Text editor

#Region "Attributes"
#Region "Private attributes"
    Private MbReadyForUse As Boolean = False 'indicates proper creation or first initialization of usercontrol
    Private MbInitOfText As Boolean = False 'text is changing = semaphore - for ex. during initializing
    Private MsOriginalRTFText As String = "" 'original RTF text - should always be in RTF format
    Private MsNewRTFText As String = "" 'new changed RTF text - should always be in RTF format
    Private MsOriginalPlainText As String = "" 'original plain text
    Private MsNewPlainText As String = "" 'new changed plain text
#End Region
#End Region
#Region "Constructors & Init"
    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.


        '**********************************************************************
        InitControl()
        '**********************************************************************
    End Sub
    'setting usercontrol settings before its using
    Private Function InitControl() As GeActionResult
        MbInitOfText = True
        SetReadyForUse(False) 'disabling usercontrol
        Try
            MbInitOfText = False
            barBtnDiscardChanges.Enabled = False
            barBtnConfirmChanges.Enabled = False
            ReadOnlyMode = False
            SetReadyForUse(True) 'enabling usercontrol
            Return GeActionResult.kkOk
        Catch ex As Exception
            MbInitOfText = False
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'initialization of controls
    Public Function Init(ByVal IsRtfText As String) As GeActionResult
        MbInitOfText = True
        Try
            If MbReadyForUse Then
                If IsRtfText Is Nothing Then IsRtfText = ""
                'creating a new document
                recRichEditControl.CreateNewDocument()

                'filling with text
                Try
                    recRichEditControl.RtfText = IsRtfText
                Catch ex As Exception
                    recRichEditControl.Text = IsRtfText
                End Try
                MsOriginalRTFText = recRichEditControl.RtfText
                MsNewRTFText = recRichEditControl.RtfText

                barBtnDiscardChanges.Enabled = False
                barBtnConfirmChanges.Enabled = False

                MbInitOfText = False
                Return GeActionResult.kkOk
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            MbInitOfText = False
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "Properties"
#Region "Public properties"
    Public ReadOnly Property ReadyForUse() As Boolean
        Get
            Return MbReadyForUse
        End Get
    End Property
    Public ReadOnly Property InitOfText() As Boolean
        Get
            Return MbInitOfText
        End Get
    End Property
    'RTF text - result of this "control"
    Public ReadOnly Property ResultRTFText() As String
        Get
            Return MsNewRTFText
        End Get
    End Property
    'plain text - result of this "control"
    Public ReadOnly Property ResultPlainText() As String
        Get
            Return MsNewPlainText
        End Get
    End Property
    'actual RTF text
    Public ReadOnly Property ResultActualRTFText() As String
        Get
            Return recRichEditControl.RtfText
        End Get
    End Property
    'actual plain text
    Public ReadOnly Property ResultActualPlainText() As String
        Get
            Return recRichEditControl.Text
        End Get
    End Property
    'readonly mode
    Public Property ReadOnlyMode() As Boolean
        Get
            Return recRichEditControl.ReadOnly
        End Get
        Set(ByVal IbValue As Boolean)
            recRichEditControl.ReadOnly = IbValue

            'barBtnNewFile.Enabled = Not ReadOnlyMode
            'barBtnOpenFile.Enabled = Not ReadOnlyMode
            barBtnConfirmChanges.Enabled = Not ReadOnlyMode
            barBtnDiscardChanges.Enabled = Not ReadOnlyMode
        End Set
    End Property
#End Region
#End Region
#Region "Events"
    Public Event MMValueChangesConfirmed(ByVal NewValue As String) 'event is raised after value of text is confirmed
    Public Event MMValueChangesDiscarded(ByVal NewValue As String) 'event is raised after value of text is discarded
    Public Event MMTextChanging() 'event is raised when text is changing
#End Region
#Region "Events handler"
    'event to notify that text is changing
    Private Sub recRichEditControl_ContentChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles recRichEditControl.ContentChanged
        ReflectAcceptDiscardChangesEnableButtons()
        RaiseEvent MMTextChanging()
    End Sub
    'setting default setting after new document is created
    Private Sub recRichEditControl_EmptyDocumentCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles recRichEditControl.EmptyDocumentCreated
        Dim charProperties As CharacterProperties = recRichEditControl.Document.BeginUpdateCharacters(recRichEditControl.Document.Range)
        charProperties.FontName = "Calibri"
        charProperties.FontSize = 12
        recRichEditControl.Document.EndUpdateCharacters(charProperties)
    End Sub
    'confirmation of changes on lost focus
    Private Sub recRichEditControl_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles recRichEditControl.LostFocus
        ConfirmChanges()
    End Sub
    'confirmation of changes
    Private Sub barBtnConfirmChanges_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles barBtnConfirmChanges.ItemClick
        ConfirmChanges()
    End Sub
    'discarding changes
    Private Sub barBtnDiscardChanges_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles barBtnDiscardChanges.ItemClick
        DiscardingChanges()
    End Sub
#End Region
#Region "Subs"
    'sets MbReadyForUse attribute and sets Enable property of usercontrol
    Private Sub SetReadyForUse(ByVal IbValue As Boolean)
        MbReadyForUse = IbValue
        Me.Enabled = IbValue 'enabling usercontrol
    End Sub
    'reflects state of changes - setting enable/disable of accpet and discard changes buttons
    Private Sub ReflectAcceptDiscardChangesEnableButtons()
        If MbInitOfText = False Then
            Dim LbWasChange As Boolean = MsOriginalRTFText <> recRichEditControl.RtfText
            barBtnDiscardChanges.Enabled = LbWasChange
            barBtnConfirmChanges.Enabled = LbWasChange
        End If
    End Sub
    'confirmation of changes
    Public Sub ConfirmChanges()
        MsNewRTFText = recRichEditControl.RtfText
        MsOriginalRTFText = recRichEditControl.RtfText
        MsNewPlainText = recRichEditControl.Text
        MsOriginalPlainText = recRichEditControl.Text

        ReflectAcceptDiscardChangesEnableButtons()

        RaiseEvent MMValueChangesConfirmed(MsNewRTFText)
    End Sub
    'discarding of changes
    Public Sub DiscardingChanges()
        Try
            recRichEditControl.RtfText = MsOriginalRTFText
        Catch ex As Exception
            recRichEditControl.Text = MsOriginalRTFText
        End Try

        ReflectAcceptDiscardChangesEnableButtons()

        RaiseEvent MMValueChangesDiscarded(MsOriginalRTFText)
    End Sub
    'clear text
    Public Sub Clear()
        Init("")
    End Sub
#End Region

End Class

 



   